package me.mstsc;

import com.alibaba.fastjson2.JSONObject;
import lombok.Getter;

import java.io.Serializable;
import java.util.Objects;

/**
 * @author langle
 * @version 1.0
 */
@Getter
public class Account implements Serializable {

    private final String computer;
    private final String address;
    private final String userName;
    private final String password;

    public Account(String computer, String userName, String password) {
        this.computer = computer;
        if (computer.contains(":")) {
            this.address = computer.substring(0, computer.indexOf(":"));
        } else {
            this.address = computer;
        }
        this.userName = userName;
        this.password = password;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Account account = (Account) o;
        return Objects.equals(getComputer(), account.getComputer()) && Objects.equals(getUserName(), account.getUserName()) && Objects.equals(getPassword(), account.getPassword());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getComputer(), getUserName(), getPassword());
    }

    @Override
    public String toString() {
        return "Account{" +
                "computer='" + computer + '\'' +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    public JSONObject toJSONObject() {
        JSONObject json = new JSONObject();
        json.put("computer", computer);
        json.put("userName", userName);
        json.put("password", password);
        return json;
    }

    public static Account fromJSONObject(JSONObject json) {
        String computer = json.getString("computer");
        String userName = json.getString("userName");
        String password = json.getString("password");
        return new Account(computer, userName, password);
    }

}
